<template>
    <!-- <div>
        商品添加
    </div> -->
    <el-form ref="add" label-width="80px" :rules="rules" :model="add">
        <el-form-item label="商品名" prop="name">
            <el-input placeholder="请输入商品" v-model="add.name"></el-input>
        </el-form-item>

        <el-form-item label="商品分类" prop="category">
            <el-select v-model="add.category" placeholder="请选择">
                <el-option :label="index.cateName" :value="index.cateName" v-for="index in categorys"
                    v-bind:key="index.id"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="商品价格" prop="price">
            <el-input-number v-model="add.price" :min="1" :max="Infinity" label="描述文字"></el-input-number>

        </el-form-item>
        <el-form-item label="商品图片" prop="imgUrl">
            <el-upload class="avatar-uploader" action="http://127.0.0.1:3001/goods/goods_img_upload"
                :on-success="success" :show-file-list=false>
                <img v-if="imgUrl" :src="BASE_URL + imgUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>

        <el-form-item label="商品描述" prop="goodsDesc">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="add.goodsDesc">
            </el-input>
        </el-form-item>


        <el-button type="primary" @click="additem"> {{ type=== 'add' ? '添加商品' : '修改商品' }} </el-button>
    </el-form>
</template>

<script>
// import Panel from '../../components/panel/Panel.vue'
import { categoriesReq } from '@/api/account.api'
import { BASE_URL } from '@/utils/common';
export default {
    props: {
        type: {
            type: String,
            default: 'add'
        },
        defaultdata: {
            type: Object,
            default: () => ({})
        }
    },
    data() {
        return {
            BASE_URL,
            imgUrl: '',

            categorys: [],
            add: {
                name: '',
                goodsDesc: '',
                price: null,
                category: '',
                imgUrl: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入商品名', trigger: 'blur' }
                ],
                category: [
                    { required: true, message: '请输入商分类', trigger: 'change' }
                ],
                price: [
                    { required: true, message: '请输入商品价格', trigger: 'blur' }
                ],
                goodsDesc: [
                    { required: true, message: '请输入商品描述', trigger: 'blur' }
                ],
                imgUrl: [
                    { required: true, message: '请添加商品图片', trigger: 'blur' }
                ]

            }
        }
    },

    methods: {

        success(response) {
            console.log(response);
            let imgUrls = response.imgUrl
            this.imgUrl = imgUrls
            this.add.imgUrl = imgUrls
            this.$refs.add.validateField('imgUrl')

        },

        additem() {
            this.add.imgUrl = this.imgUrl.split('/').pop()
            this.$refs.add.validate((valid) => {
                if (valid) {
                    console.log(valid);
                    this.$emit('success', this.add)

                } else {

                }
            })
        }
    },
    created() {
        categoriesReq().then((res) => {
            console.log(res.data);
            this.categorys = res.data
        })


    },
    watch: {
        defaultdata: {
            handler: function () {
                if (this.type === 'edit') {
                    this.add = { ...this.defaultdata }
                    this.add.imgUrl = BASE_URL + this.defaultdata.imgUrl
                    console.log(this.add, '--------------');
                }
            },
            immediate: true
        }
    }

}
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
}
</style>